<?php

class LoginController extends Controller
{
	public $defaultAction = 'login';
        public $layout = 'application.views.layouts.main';

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
                //Yii::app()->user->setFlash('loginMessage',"Data saved!");
		if (Yii::app()->user->isGuest) {
			$model=new UserLogin;
			// collect user input data
			if(isset($_POST['UserLogin']))
			{
				$model->attributes=$_POST['UserLogin'];
				// validate user input and redirect to previous page if valid
				if($model->validate()) {
					$this->lastVisited();
					if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)
                                            {
                                                if (Yii::app()->getModule('user')->isSupplier()) {
                                                    $this->redirect(Yii::app()->controller->module->returnSupplierUrl);
                                                }
                                                
                                                 elseif (Yii::app()->getModule('user')->isAdmin()) {
                                                    $this->redirect(Yii::app()->controller->module->returnAdminUrl);
                                                }
                                                 elseif (Yii::app()->getModule('user')->isTransporter()) {
                                                    $this->redirect(Yii::app()->controller->module->returnTransporterUrl);
                                                }
                                               
                                                 elseif (Yii::app()->getModule('user')->isDistributor()) {
                                                    $this->redirect(Yii::app()->controller->module->returnDistributorUrl);
                                                }
                                                else
                                                    $this->redirect(Yii::app()->controller->module->returnUrl);
                                            }
					else
						{
                                            if (Yii::app()->getModule('user')->isSupplier()) {
                                                    $this->redirect(Yii::app()->controller->module->returnSupplierUrl);
                                                }
                                                
                                                 elseif (Yii::app()->getModule('user')->isAdmin()) {
                                                    $this->redirect(Yii::app()->controller->module->returnAdminUrl);
                                                }
                                                 elseif (Yii::app()->getModule('user')->isTransporter()) {
                                                    $this->redirect(Yii::app()->controller->module->returnTransporterUrl);
                                                }
                                               
                                                 elseif (Yii::app()->getModule('user')->isDistributor()) {
                                                    $this->redirect(Yii::app()->controller->module->returnDistributorUrl);
                                                }
                                                else
                                                    $this->redirect(Yii::app()->controller->module->returnUrl);
                                                }
				}
			}
			// display the login form
			$this->render('/user/login',array('model'=>$model));
		} else
			$this->redirect(Yii::app()->controller->module->returnUrl);
	}
	
	private function lastVisited() {
		$lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);
		$lastVisit->lastvisit = time();
		$lastVisit->save();
	}

}